package com.aistar.serviceVO.impl;

import com.aistar.mapper.ContestMessageMapper;
import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.ContestMessage;
import com.aistar.pojo.ContestMessageExample;
import com.aistar.pojoVO.ContestRankVO;
import com.aistar.serviceVO.ContestRankVOService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ContestRankVOServiceImpl implements ContestRankVOService {
    private ContestMessageMapper contestMessageMapper;
    private CustomerMapper customerMapper;

    @Autowired
    public ContestRankVOServiceImpl(ContestMessageMapper contestMessageMapper, CustomerMapper customerMapper) {
        this.contestMessageMapper = contestMessageMapper;
        this.customerMapper = customerMapper;
    }


    //获取排名(前十名)可以采取分页  查询第一页，不给出上一页下一页即可
    @Override
    public ServerResponse getRank(Integer contestId,Integer pageSize) {

        Page<ContestMessage> contestMessages = PageHelper.startPage(1,pageSize);
        ContestMessageExample example = new ContestMessageExample();
        example.createCriteria().andStatusEqualTo(1).andContestIdEqualTo(contestId);
        example.setOrderByClause("'contest_msg_mark' ASC,contest_msg_through_time DESC");//排序要求
        List<ContestMessage> contestMessageList = contestMessageMapper.selectByExample(example);
        Set<ContestRankVO> contestRankVOList = new HashSet<>();
        for (ContestMessage contestMessage:contestMessageList){
            String customerName = customerMapper.selectByPrimaryKey(contestMessage.getCustomerId()).getCustomerName();
            ContestRankVO contestRankVO = new ContestRankVO();
            contestRankVO.setCustomerName(customerName);
            contestRankVO.setContestMsgMark(contestMessage.getContestMsgMark());
            contestRankVO.setContestMsgThroughTime(contestMessage.getContestMsgThroughTime());
            contestRankVOList.add(contestRankVO);
        }
        if (contestRankVOList.size()>0)
            return ServerResponse.getSuccess(contestRankVOList);
        return ServerResponse.getFailed();
    }
}
